package com.jkoa.stripes.action;

import able.stripes.JSP;
import com.google.inject.Inject;
import com.jkoa.BaseActionBean;
import com.jkoa.dao.KodPgunaDAO;
import com.jkoa.mgr.UserManager;
import com.jkoa.model.KodPguna;
import com.jkoa.model.Pengguna;
import com.jkoa.stripes.config.AuthRequired;
import java.util.Date;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import org.apache.log4j.Logger;
import java.util.List;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.security.action.Secure;

/**

 * @author wan.fairul
 *
 */
@UrlBinding("/daftar/pengguna")
@AuthRequired
@Secure(roles = "AD")
public class RegisterUserActionBean extends BaseActionBean {

    private static final Logger logger = Logger.getLogger(RegisterUserActionBean.class);
    private Pengguna pguna;
    @Inject
    UserManager userManager;
    @Inject
    KodPgunaDAO kodPgunaDAO;
    private List<Pengguna> senaraiPguna;
    private String passwordGenerate = "";
    public static final int password_Length = 8;
    protected static java.util.Random random = new java.util.Random();
    protected static char[] goodChar = {'a', 'b', 'c', 'd', 'e', 'f', 'g',
        'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
        'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
        'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-', '@'};

    @DefaultHandler
    public Resolution showForm() {
        pguna = new Pengguna();
        senaraiPguna = userManager.findAll();
        return new JSP("daftar_pguna.jsp");
    }

    @Before(stages = {LifecycleStage.BindingAndValidation})
    public void rehydrate() {
        senaraiPguna = userManager.findAll();
    }

    public Resolution savePguna() {
        try {
            Pengguna p = userManager.findByUserId(pguna.getIdPengguna());
            if (p == null) {
                p = new Pengguna();
                p.setAktif(pguna.getAktif());
                p.setPassword(pguna.getPassword());
                p.setNama(pguna.getNama());
                if (pguna.getKodPguna() != null) {
                    KodPguna kp = kodPgunaDAO.findById(pguna.getKodPguna().getKod());
                    p.setKodPguna(kp);
                }

                p.setIdPengguna(pguna.getIdPengguna());
                p.setDimasukOleh(getUserId());
                p.setTrhMasuk(new Date());
                userManager.savePengguna(p);
                addSimpleMessage("Maklumat telah berjaya disimpan");
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        rehydrate();
        return showForm();
    }

    public Resolution deletePguna() {
        String idPengguna = getContext().getRequest().getParameter("idPengguna");
        logger.info(idPengguna);
        if (idPengguna != null) {
            Pengguna p = userManager.findByUserId(idPengguna);
            if (p != null) {
                userManager.deletePengguna(p);
            }
        }
        rehydrate();
        return new RedirectResolution(RegisterUserActionBean.class, "showForm");
    }

    public Resolution janaPassword() {
        String result = "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < password_Length; i++) {
            sb.append(goodChar[random.nextInt(goodChar.length)]);
        }
        passwordGenerate = sb.toString();        
        result = passwordGenerate;
        return new StreamingResolution("text/plain", result);
    }

    public Pengguna getPguna() {
        return pguna;
    }

    public void setPguna(Pengguna pguna) {
        this.pguna = pguna;
    }

    public List<Pengguna> getSenaraiPguna() {
        return senaraiPguna;
    }

    public void setSenaraiPguna(List<Pengguna> senaraiPguna) {
        this.senaraiPguna = senaraiPguna;
    }
}
